import { SlotType } from "./enum";
import { y3Ability } from "./y3Ability";
import { y3Item } from "./y3Item";
import { y3Player } from "./y3Player";
import { y3Unit } from "./y3Unit";

export class y3UI {
    player: y3Player
    handle: string
    name: string

    constructor(player: y3Player, ui_name: string) {
        this.player = player
        this.handle = ui_name
        this.name = GameAPI.get_ui_comp_name(player.handle, ui_name)
    }

    static getByHandle(this: void, player: y3Player, ui_name: string) {
        return new y3UI(player, ui_name)
    }

    static createUI(this: void, player: y3Player, parent_ui: y3UI, comp_type: number) {
        let pyUi = GameAPI.create_ui_comp(
            player.handle,
            parent_ui.handle,
            comp_type
        )
        return y3UI.getByHandle(player, pyUi)
    }

    static getUI(this: void, player: y3Player, ui_path: string) {
        let pyUI = GameAPI.get_comp_by_absolute_path(player.handle, ui_path)
        return y3UI.getByHandle(player, pyUI)
    }

    toString() {
        return `{UI|${this.name}|${this.handle}}@${this.player.toString()}`
    }

    addEvent(event: number, name: string) {
        GameAPI.create_ui_comp_event_ex_ex(this.handle, event, name)
    }

    sendEvent(eventName: string) {
        GameAPI.trigger_ui_event(this.player.handle, this.handle, eventName)
    }

    set visible(isVisible: boolean) {
        GameAPI.set_ui_comp_visible(
            this.player.handle,
            isVisible,
            this.handle
        )
    }

    get visible() {
        return GameAPI.get_ui_comp_visible(this.player.handle, this.handle)
    }

    /**
     * 设置控件的图片
     */
    set image(img: py.Texture) {
        GameAPI.set_ui_comp_image_with_icon(
            this.player.handle,
            this.handle,
            img
        )
    }
    /**
     * 设置控件的文本
     */
    set text(str: string) {
        GameAPI.set_ui_comp_text(
            this.player.handle,
            this.handle,
            str
        )
    }
    /**
     * 设置控件的透明度
     */
    set alpha(value: number) {
        GameAPI.set_ui_comp_opacity(
            this.player.handle,
            this.handle,
            value
        )
    }
    /**
     * 设置控件是否可以拖动
     */
    set draggable(isDrag: boolean) {
        GameAPI.set_ui_comp_drag(
            this.player.handle,
            this.handle,
            isDrag
        )
    }

    /**
     * 设置控件是否拦截操作
     */
    set intercepts(isIntercepts: boolean) {
        GameAPI.set_ui_comp_swallow(
            this.player.handle,
            this.handle,
            isIntercepts
        )
    }

    /**
     * 设置控件的层次
     */
    set zIndex(deep: number) {
        GameAPI.set_ui_comp_z_order(
            this.player.handle,
            this.handle,
            deep
        )
    }

    /**
     * 设置进度条最大值
     */
    set progressMaxValue(maxValue: number) {
        GameAPI.set_progress_bar_max_value(
            this.player.handle,
            this.handle,
            maxValue
        )
    }

    /**
     * 设置进度条当前值
     * @param value 进度条的值
     * @param time  渐变时间
     */
    setProgressCurrentValue(value: number, time?: number) {
        GameAPI.set_progress_bar_current_value(
            this.player.handle,
            this.handle,
            value,
            time
        )
    }

    /**
     * 启用/禁用按钮
     */
    set buttonEnable(enable: boolean) {
        GameAPI.set_ui_comp_enable(
            this.player.handle,
            this.handle,
            enable
        )
    }

    setSize(width: number, height: number) {
        GameAPI.set_ui_comp_size(
            this.player.handle,
            this.handle,
            width,
            height
        )
    }

    set fontSize(size: number) {
        GameAPI.set_ui_comp_font_size(
            this.player.handle,
            this.handle,
            size
        )
    }
    /**
     * 获取焦点
     */
    setFocus() {
        GameAPI.set_input_field_focus(
            this.player.handle,
            this.handle
        )
    }

    /**
     * 绑定技能
     * @param skill 
     */
    bindSkill(skill: y3Ability) {
        GameAPI.set_skill_on_ui_comp(
            this.player.handle,
            skill.handle,
            this.handle
        )
    }

    /**
     * 绑定单位的Buff
     * @param unit 
     */
    bindBuffUnit(unit: y3Unit) {
        GameAPI.set_buff_on_ui_comp(
            this.player.handle,
            unit.handle,
            this.handle
        )
    }

    /**
     * 绑定物品
     * @param item 
     */
    bindItem(item: y3Item) {
        GameAPI.set_item_on_ui_comp(
            this.player.handle,
            item.handle,
            this.handle
        )
    }

    static setPrefabVisible(this: void, player: y3Player, visible: boolean) {
        GameAPI.set_prefab_ui_visible(
            player.handle,
            visible
        )
    }

    set model(modelId: py.Model) {
        GameAPI.set_ui_model_id(
            this.player.handle,
            this.handle,
            modelId
        )
    }

    static setMiniMap(player: y3Player, img: py.Texture) {
        GameAPI.change_mini_map_img(
            player.handle,
            img
        )
    }

    /**
     * 绑定单位物品栏
     * @param unit 
     * @param field 
     * @param index 
     */
    bindUnitSlot(unit: y3Unit, field: SlotType, index: number) {
        GameAPI.set_ui_comp_unit_slot(
            this.player.handle,
            this.handle,
            unit.handle,
            field,
            index
        )
    }

    /**
     * 设置按钮快捷键
     */
    set shortCut(key: number) {
        GameAPI.set_btn_short_cut(
            this.player.handle,
            this.handle,
            key
        )
    }

    /**
     * 设置按钮组合快捷键
     */
    set metakey(key: number) {
        GameAPI.set_btn_func_short_cut(
            this.player.handle,
            this.handle,
            key
        )
    }

    /**
     * 设置智能是否快捷键
     */
    set skillSmartKey(key: number) {
        GameAPI.set_skill_btn_smart_cast_key(
            this.player.handle,
            this.handle,
            key
        )
    }

    /**
     * 设置智能组合快捷键
     */
    set skillMetaKey(key: number) {
        GameAPI.set_skill_btn_func_smart_cast_key(
            this.player.handle,
            this.handle,
            key
        )
    }

    /**
     * 播放/停止技能按钮特效
     */
    set skillActionEffect(isOpen: boolean) {
        GameAPI.set_skill_btn_action_effect(
            this.player.handle,
            this.handle,
            isOpen
        )
    }

    setTextColor(r: number, g: number, b: number, a: number) {
        GameAPI.set_ui_comp_font_color(
            this.player.handle,
            this.handle,
            r, g, b, a
        )
    }

    setShowRoomFov(fov: number) {
        GameAPI.change_showroom_fov(
            this.player.handle,
            this.handle,
            fov
        )
    }

    setShowRoomCposition(x: number, y: number, z: number) {
        GameAPI.change_showroom_cposition(
            this.player.handle,
            this.handle,
            x, y, z
        )
    }

    setShowRoomCrotation(x: number, y: number, z: number) {
        GameAPI.change_showroom_crotation(
            this.player.handle,
            this.handle,
            x, y, z
        )
    }
}